﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using Advisor.Entities;
using System.Collections;
using NHibernate.Criterion;

namespace Advisor.Entities
{
  public class MembersDAO
  {
      private Users m_User;
    public MembersDAO(Users user)
    {
        this.m_User = user;
    }
    /// <summary>
    /// Retorna todas os registros da tabela Members. 
    /// </summary>
    /// <param name="column"></param>
    /// <param name="maximumRows"></param>
    /// <param name="startRowIndex"></param>
    /// <returns></returns>
    internal IList GetMembers(FilterCollection filters)
    {
        ISession session = NHibernateSession.GetSession();
        ICriteria c = session.CreateCriteria(typeof(Members));

        foreach (ICriterion filter in filters)
        {
            c.Add(filter);
        }
        return c.List();
    }


    /// <summary>
    ///  Método para inserir um Member no banco. 
    /// </summary>
    /// <param name="member"></param>
    internal void InsertMember(Members member)
    {
      ISession session = NHibernateSession.GetSession();
      session.Save(member);
      session.Close();
    }
    /// <summary>
    /// Retorna uma instância da tabela Member passando o id;
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    internal Members GetMember(FilterCollection filters)
    {
        ISession session = NHibernateSession.GetSession();
        ICriteria c = session.CreateCriteria(typeof(Members));

        foreach (ICriterion filter in filters)
        {
            c.Add(filter);
        }
        return c.UniqueResult<Members>();
    }

    /// <summary>
    /// Remove um member do banco
    /// </summary>
    /// <param name="member"></param>
    internal void DeleteMember(Members member)
    {
      ISession sessions = NHibernateSession.GetSession();
      sessions.Delete(member);
      sessions.Close();
    }

    /// <summary>
    /// Atualiza um member no banco.
    /// </summary>
    /// <param name="member"></param>
    internal void UpdateMember(Members member)
    {
      ISession sessions = NHibernateSession.GetSession();
      sessions.Update(member);
      //Comando que ativa as alterações no banco de dados. 
      sessions.Flush();
      sessions.Close();
    }



  }
}
